package com.iris.live.services.services.impl;

import com.iris.live.services.common.JsonObjectUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.common.reconsitution.DefaultValueConstant;
import com.iris.live.services.data.models.GroupAreaModel;
import com.iris.live.services.data.models.用户Model;
import com.iris.live.services.data.repositories.GroupAreaRepository;
import com.iris.live.services.data.repositories.Lsh车型Repository;
import com.iris.live.services.data.repositories.用户Repository;
import com.iris.live.services.services.BrandService;
import com.iris.live.services.vo.BrandVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by yitianle on 2017/9/25.
 */
@Service
@Transactional(readOnly = true)
public class BrandServiceImpl implements BrandService {
    private static final Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);

    @Autowired
    private Lsh车型Repository lsh车型Repository;

    @Autowired
    private 用户Repository 用户Repository;
    @Autowired
    private GroupAreaRepository groupAreaRepository;


    @Override
    public List<String> getBrandListByUser(String username) {
        Set<String> brands = brands = getBrandByUsername(username);
        if(brands == null || brands.isEmpty()) {
            用户Model userModel = 用户Repository.getOne(username);
            GroupAreaModel groupAreaModel = groupAreaRepository.getInfoByPermit(userModel.get许可());
            if(groupAreaModel.getParentId() == 0) { //集团
                return lsh车型Repository.selectAllBrands();
            }else{
                brands = this.getBrandByPermit(groupAreaModel);
            }
        }
        if(brands != null && logger.isDebugEnabled()){
            logger.debug("brands={}", JsonObjectUtils.createJsonString(brands));
        }

        if(brands == null || brands.isEmpty()){
            return lsh车型Repository.selectAllBrands();
        }else {
            return new ArrayList<>(brands);
        }
    }

    @Override
    public List<String> getBrandListByPermit(String permit) {
        GroupAreaModel groupAreaModel = groupAreaRepository.getInfoByPermit(permit);

        return this.getBrandListByArea(groupAreaModel);
    }

    @Override
    public List<String> getBrandListByArea(GroupAreaModel groupAreaModel) {
        Set<String> brands = this.getBrandByPermit(groupAreaModel);
        if(brands != null && logger.isDebugEnabled()){
            logger.debug("brands={}", JsonObjectUtils.createJsonString(brands));
        }

        if(brands == null || brands.isEmpty()){
            return lsh车型Repository.selectAllBrands();
        }else {
            return new ArrayList<>(brands);
        }
    }

    /**
     * 根据登录账户查询品牌
     * @param username
     * @return
     */
    private Set<String> getBrandByUsername(String username){
        用户Model user = 用户Repository.findModelByUsername(username);
        if(null != user && !StringUtils.isNullOrBlank(user.get品牌())){
            return getBrandArr(user.get品牌());
        }

        return null;
    }

    /**
     * 根据门店许可查询品牌
     * @param permitModel
     * @return
     */
    private Set<String> getBrandByPermit(GroupAreaModel permitModel){
        Set<String> list = new HashSet<>();
        if(permitModel == null){
            logger.error("GroupAreaModel参数为null。");
            return list;
        }

        //根据当前许可的品牌设置获取品牌许可,这直接获取
        if(StringUtils.isNotNullOrBlank(permitModel.getBrand())){
            list = this.getBrandArr(permitModel.getBrand());
        }else{
            //许可本身当前没有设置品牌许可信息，则从下属许可获取品牌信息
            List<GroupAreaModel> permitModelList = groupAreaRepository.getAreaListByParentId(permitModel.getId());
            for (int i = 0; i < permitModelList.size(); i++){
                GroupAreaModel childModel = permitModelList.get(i);
                list.addAll(getBrandByPermit(childModel));
            }
        }

        return list;
    }

    /**
     * 分割品牌返回集合
     * @param brandStr
     * @return
     */
    private Set<String> getBrandArr(String brandStr){
        Set<String> brands = new HashSet<>();

        if(!StringUtils.isNullOrBlank(brandStr)){
            String[] str = brandStr.split(DefaultValueConstant.SPLIT_STR);
            for(int i = 0; i < str.length; i++){
                brands.add(str[i]);
            }
        }
        return brands;
    }

    @Override
    public List<String> getBrandListByType(String permit, String userName, Integer queryType) {
        if (null != queryType && 2 == queryType) {  // 获取地域的品牌
            return this.getBrandListByPermit(permit);
        } else {
            return this.getBrandListByUser(userName); // 获取用户的品牌
        }
    }
}
